package phi.engine.scene 
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	
	import phi.engine.PhiEngine;
	import phi.engine.actors.IActor;
	import phi.engine.core.GameTime;
	import phi.engine.core.IGame;
	import phi.engine.layers.ILayer;
	import phi.engine.layers.Layer;

	/**
	 * ...
	 * @author Alexandru Ghiura
	 */
	public class SceneManager 
	{
		// Protected vars
		protected var engine :PhiEngine = PhiEngine.getInstance();
		
		// Private vars
		private var _layers :Array = [];
		private var _defaultLayer :ILayer;
		
		public function SceneManager() 
		{
		}
		
		public function get defaultLayer():ILayer
		{
			return _defaultLayer;
		}
		
		public function get layers():Array
		{
			return _layers.concat();
		}
		
		public function set defaultLayer( value:ILayer ):void
		{
			// if we have this layer
			if( _layers.indexOf( value ) != -1 )
				_defaultLayer = value;
		}
		
		public function addLayer( layer:ILayer, setAsDefault:Boolean=false ):void
		{
			if( !layer )
				return;
			
			if( _layers.indexOf( layer ) == -1 )
			{
				_layers.push( layer );
				
				if( setAsDefault || defaultLayer == null )
					_defaultLayer = layer;
				
				// Add layer to stage
				engine.game.window.addChild( DisplayObject( layer ));
			}
		}
		
		public function update( gameTime:GameTime ):void
		{
			for each( var layer :ILayer in layers )
				layer.update( gameTime );
		}
		
		public function handleInput():void
		{
			for each( var layer :ILayer in layers )
				layer.handleInput();
		}
	}

}